<template>
  <el-container class="home-container">
    <el-header>
      <div class="title">
        <img src="../assets/logo.png"
             alt="">
        <span>VD电商管理系统</span>
      </div>
      <el-button type="info"
                 @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '230px'">
        <div class="toggle-btn"
             @click="toggleCollapse">|||</div>
        <el-menu background-color="#545c64"
                 text-color="#fff"
                 active-text-color="#1cc09f"
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 router
                 :default-active="activePath">
          <el-menu-item index="/home"
                        @click="saveNavState('/home')">
            <i class="iconfont icon-home"></i>
            <span>主页</span>
          </el-menu-item>
          <el-submenu :index="item.id + ''"
                      v-for="item in menuList"
                      :key="item.id">
            <template slot="title">
              <i :class="menuicons[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+subItem.path + ''"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <i class="el-icon-caret-right"></i>
              <span>{{subItem.authName}}</span>

            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      isCollapse: false,
      activePath: '',
      menuList: [],
      menuicons: {
        101: 'iconfont icon-goods',
        102: 'iconfont icon-detail',
        103: 'iconfont icon-permission',
        125: 'iconfont icon-user',
        145: 'iconfont icon-tongji'
      }
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    // 折叠侧边菜单
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 侧边菜单高亮
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #36373c;
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .title {
    display: flex;
    align-items: center;
    width: 230px;
    color: #fff;
    font-size: 22px;
    img {
      width: 55px;
      margin-right: 10px;
    }
  }
}
.el-aside {
  background-color: #545c64;
  .el-menu {
    border-right: none;
  }
}
.iconfont {
  vertical-align: middle;
  margin-right: 5px;
  width: 24px;
  text-align: center;
  font-size: 20px;
  speak: none;
  font-style: normal;
  font-weight: 400;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  vertical-align: baseline;
  display: inline-block;
  -webkit-font-smoothing: antialiased;
}
.toggle-btn {
  background-color: #4a5064;
  color: #fff;
  text-align: center;
  font-size: 12px;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-main {
  background-color: #e6e6e6;
}
</style>
